package com.cwd.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.cwd.dao.PermissionDao;
import com.cwd.dao.RoleDao;
import com.cwd.dao.UserDao;
import com.cwd.pojo.Permission;
import com.cwd.pojo.Role;
import com.cwd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @PackageName: com.cwd.service
 * @ClassName: UserServiceImpl
 * @Author: CWD
 * @Date: 2020/3/12 21:54
 * @Description:
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao ;

    //根据用户名查询用户信息(基本信息，角色，权限)
    public User findByUsername(String usrename) {
        User user = userDao.findByUsername(usrename);   //用户的基本信息
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }
}
